home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / SHARED.H < prev    next >
C/C++ Source or Header  |  1992-08-12  |  2KB  |  89 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: VDN 03/25/92 -- Initial design
  13. // Updated: JAM 08/12/92 -- removed 'static' from static memb func def
  14. //
  15. // Shared is a mixin slot, which store the number of references and 
  16. // handles pointed at an object. Shared does not have a virtual destructor,
  17. // and so the deletion of the object must be done by the caller of dereference.
  18.  
  19. #ifndef SHAREDH
  20. #define SHAREDH
  21.  
  22. #ifndef MISCH
  23. #include <misc.h>                // for INVALID marker
  24. #endif
  25.  
  26. class CoolShared {
  27. public:
  28.   inline CoolShared();                // Initialize ref_count=0
  29.   inline ~CoolShared();                // Destructor
  30.  
  31.   inline int reference_count();            // query current ref_count
  32.   inline int reference();            // inc ref_count
  33.   inline int dereference();            // dec ref_count
  34.   
  35.   static inline int reference(CoolShared* ptr);    // noop if ptr=NULL
  36.   static inline int dereference(CoolShared* ptr);
  37.  
  38. private:
  39.   int ref_count;                // count of active references
  40. };
  41.  
  42. // CoolShared() -- Default constructor initializes ref_count to 0
  43.  
  44. inline CoolShared::CoolShared()
  45. : ref_count(0) {}                // ref_count initially 0
  46.  
  47. // ~CoolShared -- Destructor is not virtual, so caller of dereference
  48. //               must call delete on object ptr, with correct type.
  49.  
  50. inline CoolShared::~CoolShared() {}        // nothing
  51.  
  52. // reference_count() -- Query current ref_count
  53.  
  54. inline int CoolShared::reference_count() {
  55.   return ref_count;
  56. }
  57.  
  58. // reference() -- increment ref_count and return new count.
  59.  
  60. inline int CoolShared::reference() {
  61.   return ++ref_count;
  62. }
  63.  
  64. // dereference() -- decrement ref_count and return new count.
  65.  
  66. inline int CoolShared::dereference(){
  67.   return --ref_count;
  68. }
  69.  
  70. // Check pointers first, noop if ptr == NULL.
  71.   
  72. inline int CoolShared::reference (CoolShared* ptr) {
  73.   if (ptr)
  74.     return ptr->reference();
  75.   else
  76.     return INVALID;
  77. }
  78.  
  79. inline int CoolShared::dereference (CoolShared* ptr) {
  80.   if (ptr)
  81.     return ptr->dereference();
  82.   else
  83.     return INVALID;
  84. }
  85.  
  86.  
  87.  
  88. #endif                        // SHAREDH
  89.